package com.example.testapplication.rolling;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.testapplication.R;

import org.jetbrains.annotations.NotNull;

import java.util.Random;

/**
 * 字控件滚动平台
 */
public class RollingPlatform extends ViewGroup {
    private int gravity = Rolling.left;
    private OnChildClickListener onChildClickListener;
    private final Random random = new Random();
    private final int distance = 5;

    public RollingPlatform(Context context) {
        super(context);
    }

    public RollingPlatform(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public RollingPlatform(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnChildClickListener(OnChildClickListener onChildClickListener) {
        this.onChildClickListener = onChildClickListener;
    }

    public void setGravity(int gravity) {
        if (gravity == Rolling.left || gravity == Rolling.right) {
            this.gravity = gravity;
        }
    }

    public int getGravity() {
        return gravity;
    }

    public void addRollingView(View view) {
        addView(view);
    }

    public void addRollingView(View view, @NotNull RollingStatus status) {
        view.setTag(R.id.data_id, status);
        addView(view);
    }

    @Override
    public void addView(View child) {
        child.setOnClickListener(view -> {
            if (onChildClickListener != null) {
                RollingStatus status = null;
                Object obj = view.getTag(R.id.data_id);
                if (obj instanceof RollingStatus) {
                    status = (RollingStatus) obj;
                }
                onChildClickListener.clickListener(view, status);
            }
        });
        super.addView(child);

    }

    public void move() {
        if (gravity == Rolling.left) {
            moveLeft();
        } else {
            moveRight();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //测量子view的宽高
        for (int i = 0; i < getChildCount(); i++) {
            measureChild(getChildAt(i), widthMeasureSpec, heightMeasureSpec);
        }
    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        if (gravity == Rolling.left) {
            layoutLeft(b, i, i1, i2, i3);
        } else {
            layoutRight(b, i, i1, i2, i3);
        }
    }

    private void layoutLeft(boolean b, int i, int i1, int i2, int i3) {
        int count = getChildCount();
        int left, right, top, bottom;
        for (int j = 0; j < count; j++) {
            View view = getChildAt(j);
            if (view.getWidth() != 0 && view.getHeight() != 0) {
                continue;
            }
            left = -view.getMeasuredWidth();
            top = random.nextInt(i3 - view.getMeasuredHeight());
            right = left + view.getMeasuredWidth();
            bottom = top + view.getMeasuredHeight();
            view.layout(left, top, right, bottom);
        }
    }

    private void moveLeft() {
        for (int i = getChildCount() - 1; i >= 0; i--) {
            View view = getChildAt(i);
            if (view.getLeft() >= getWidth()) {
                Log.e("RollingPlatform", "left看不到了---->移除");
                removeViewAt(i);
            } else {
                RollingStatus status;
                Object obj = view.getTag(R.id.data_id);
                int speed = 0;
                if (obj instanceof RollingStatus) {
                    status = (RollingStatus) obj;
                    speed = status.speed;
                }
                view.layout(view.getLeft() + distance + speed, view.getTop(), view.getRight() + distance + speed, view.getBottom());
            }
        }
    }

    private void layoutRight(boolean b, int i, int i1, int i2, int i3) {
        int count = getChildCount();
        int left, right, top, bottom;
        for (int j = 0; j < count; j++) {
            View view = getChildAt(j);
            if (view.getWidth() != 0 && view.getHeight() != 0) {
                continue;
            }
            left = i2;
            top = random.nextInt(i3 - view.getMeasuredHeight());
            right = left + view.getMeasuredWidth();
            bottom = top + view.getMeasuredHeight();
            view.layout(left, top, right, bottom);
        }
    }

    private void moveRight() {
        for (int i = getChildCount() - 1; i >= 0; i--) {
            View view = getChildAt(i);
            if (view.getRight() <= 0) {
                Log.e("RollingPlatform", "right看不到了---->移除");
                removeViewAt(i);
            } else {
                RollingStatus status;
                Object obj = view.getTag(R.id.data_id);
                int speed = 0;
                if (obj instanceof RollingStatus) {
                    status = (RollingStatus) obj;
                    speed = status.speed;
                }
                view.layout(view.getLeft() - distance - speed, view.getTop(), view.getRight() - distance - speed, view.getBottom());
            }
        }
    }
}
